<?php
	class ListAction extends BaseAction{
		public function show(){
			if(!empty($_GET['id'])){
				//子栏目列表
				$cat = M('Category');
				$catData = $cat->field('id,pid,category_name')
								->where('pid = '.$_GET['id'])
								->select();
				//获取当前位置
				$bpath = $cat->field("concat(path,'-',id) as bpath")
							->where('id = '.$_GET['id'])
							->find();
				$path = str_replace('R', 0, $bpath['bpath']);
				$path = str_replace('-', ',', $path);
				$pathArr = $cat->field('id,category_name')
							->where('id in ('.$path.')')
							->order('path')
							->select();
				$this->assign('path',$pathArr);
				//获得文章列表
				$art = M('Article');
				import("ORG.Util.Page");
				$count = $art->where('category in '.'(select id from zgcms_category where id = '.$_GET['id'].' or pid = '.$_GET['id'].' or path like "%-'.$_GET['id'].'-%")')
							->count();
				$page = new Page($count,c('PAGESIZE'));
				$show = $page->show();
				$artData = $art->limit($page->firstRow.','.$page->listRows)
								->field('zgcms_article.id as id,category_name,title,info,category,date,count,keyword,tag,thumbnail')
								->join('zgcms_category on zgcms_article.category = zgcms_category.id')
								->order('date desc')
								->where('category in '.'(select id from zgcms_category where id = '.$_GET['id'].' or pid = '.$_GET['id'].' or path like "%-'.$_GET['id'].'-%")')
								//->where('category = '.$_GET['id'])
								->select();
				$this->assign('cat',$catData);
				$this->assign('art',$artData);
				//本月本类推荐文章 10篇
				$monthCatRecNews = $art->field('id,title,date')
										->where("DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= FROM_UNIXTIME(date,'%y-%m-%d') and attr like \"%推荐%\" and ".'category in '.'(select id from zgcms_category where id = '.$_GET['id'].' or pid = '.$_GET['id'].' or path like "%-'.$_GET['id'].'-%")')
										->limit(10)
										->select();
				getSubStr($monthCatRecNews, 'title', 12, 'utf-8');
// 				echo $art->getLastSql();
				$this->assign('monthCatRecNews',$monthCatRecNews);
				
				//本月本类热点文章，点击量最大的十篇
				$monthCatHotNews = $art->field('id,title,date')
										->where("DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= FROM_UNIXTIME(date,'%y-%m-%d') and ".'category in '.'(select id from zgcms_category where id = '.$_GET['id'].' or pid = '.$_GET['id'].' or path like "%-'.$_GET['id'].'-%")')
										->order('count desc')
										->limit(10)
										->select();
				getSubStr($monthCatHotNews, 'title', 12, 'utf-8');
				$this->assign('monthCatHotNews',$monthCatHotNews);
				
				//本月本类图文 10篇
				$monthCatPicNews = $art->field('id,title,date')
										->where("thumbnail <> '' and DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= FROM_UNIXTIME(date,'%y-%m-%d') and ".'category in '.'(select id from zgcms_category where id = '.$_GET['id'].' or pid = '.$_GET['id'].' or path like "%-'.$_GET['id'].'-%")')
										->order('date desc')
										->limit(10)
										->select();
				getSubStr($monthCatPicNews, 'title', 12, 'utf-8');
				$this->assign('monthCatPicNews',$monthCatPicNews);
				$this->assign('page',$show);
				$this->display();
			}else{
				$this->error('请选择要查看的分类！');
			}
		}
		
		public function details(){
			//获取文章内容
			$art = M('Article');
			$artData = $art->field('id,category,title,author,count,date,source,content,tag,info')
						->where('id = '.$_GET['id'])
						->find();
			if(!empty($artData)){
				//格式化tag标签内容
				$tagArr = explode(',', $artData['tag']);
				foreach($tagArr as $key => $val){
					$tagArr[$key] = '<a href="__APP__/Search/show/type/tag/inputkeyword/'.$tagArr[$key].'">'.$tagArr[$key].'</a>';
				}
				$artData['tag'] = implode(',', $tagArr);
				//点击数加1
				$art->where('id = '.$_GET['id'])->setInc('count',1);
				$artData['count']++;
				$this->assign('art',$artData);

				//获取当前位置
				$cat = M('Category');
				$bpath = $cat->field("concat(path,'-',id) as bpath")
							->where('id = '.$artData['category'])
							->find();
				$path = str_replace('R', 0, $bpath['bpath']);
				$path = str_replace('-', ',', $path);
				$pathArr = $cat->field('id,category_name')
								->where('id in ('.$path.')')
								->order('path')
								->select();
				$this->assign('path',$pathArr);
				//获取子栏目列表
				
				$catData = $cat->field('id,pid,category_name')
								->where('pid = '.$artData['category'])
								->select();
				$this->assign('cat',$catData);
				
				//获取最新评论三条评论
				$comment = M('Comment');
				$comData = $comment->field('zgcms_comment.id id,zgcms_comment.date date,content,manner,support,oppose,user,face,uid')
									->join('zgcms_user on zgcms_comment.uid = zgcms_user.id')
									->where('zgcms_comment.state = 1 and aid = '.$artData['id'])
									->order('date desc')
									->limit(3)
									->select();
				$count = $comment->where('state = 1 and aid = '.$artData['id'])
								->count();
// 				$count = is_array($comData) ? count($comData) : 0;
				$this->assign('comCount',$count);
				$this->assign('comData',$comData);
				
				//本月本类推荐文章 10篇
				$monthCatRecNews = $art->field('id,title,date')
										->where("DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= FROM_UNIXTIME(date,'%y-%m-%d') and attr like \"%推荐%\" and ".'category in '.'(select id from zgcms_category where id = '.$artData['category'].' or pid = '.$artData['category'].' or path like "%-'.$artData['category'].'-%")')
										->limit(10)
										->select();
				getSubStr($monthCatRecNews, 'title', 12, 'utf-8');
				$this->assign('monthCatRecNews',$monthCatRecNews);
// 				echo $art->getLastSql();

				//本月本类热点文章，点击量最大的十篇
				$monthCatHotNews = $art->field('id,title,date')
										->where("DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= FROM_UNIXTIME(date,'%y-%m-%d') and ".'category in '.'(select id from zgcms_category where id = '.$artData['category'].' or pid = '.$artData['category'].' or path like "%-'.$artData['category'].'-%")')
										->order('count desc')
										->limit(10)
										->select();
				getSubStr($monthCatHotNews, 'title', 12, 'utf-8');
				$this->assign('monthCatHotNews',$monthCatHotNews);
				
				//本月本类图文 10篇
				$monthCatPicNews = $art->field('id,title,date')
									->where("thumbnail <> '' and DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= FROM_UNIXTIME(date,'%y-%m-%d') and ".'category in '.'(select id from zgcms_category where id = '.$artData['category'].' or pid = '.$artData['category'].' or path like "%-'.$artData['category'].'-%")')
									->order('date desc')
									->limit(10)
									->select();
				getSubStr($monthCatPicNews, 'title', 12, 'utf-8');
				$this->assign('monthCatPicNews',$monthCatPicNews);
				$this->display();
			}else{
				$this->error('请确保文章ID正确！');
			}
			
			
		}
	}
?>